package xio.ccf_201712;

import java.util.*;


/**
 * ccf 201712-3 Crontab
 *  100分
 * Created by Xio on 2018/2/9.
 */
public class Crontab {
    public static void main(String[] args) {

        int n;
        String s, t;
        Scanner sc = new Scanner(System.in);
        n = Integer.parseInt(sc.next());
        s = sc.next();
        t = sc.next();
        //解析系统的开机和关机时间（年份）
        int startYear = (int) (Long.parseLong(s)/100000000L);
        int endYear = (int) (Long.parseLong(t)/100000000L);


        //输入配置信息
        String[][] crons = new String[n][6];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 6; j++) {
                crons[i][j] = sc.next();
            }
        }
        sc.close();

        //初始化月份和星期的字符串映射
        for (int i = 0; i < MONTHS.length; i++) {
            dateMap.put(MONTHS[i],i+1);
        }
        for (int i = 0; i < WEEKS.length; i++) {
            dateMap.put(WEEKS[i],i);
        }

        PriorityQueue<CronCmd> pq = new PriorityQueue<>(); //用于结果存储（自动排序）
        Set<Long> preList = new HashSet<>();
        for (int i = 0; i < n; i++) {
            Long mul = 1L;
            //j=0,1,2,3,4 分别对应一条配置信息的分钟、时、日、月、星期
            for (int j = 0; j < 5; j++) {
                Set<Long> valid = new HashSet<>();
                setValue(valid, crons[i][j], j);
                switch (j) {
                    case 0:
                        preList.clear();
                        preList.addAll(valid);
                        continue;
                    case 4:
                        mul *= 100L; //此时倍数应该是100_00_00_00
                        boolean[] dws = new boolean[7]; //记录是星期几要进行配置
                        for (Long a : valid) {
                            dws[a.intValue()] = true;
                        }
                        for (Long b : preList) {
                            int month = (int) (b / 1000000L);
                            int day = (int) (b / 10000L % 100);
                            for (int k = startYear; k <= endYear; k++) {
                                int dw = getDayofWeek(k, month, day);
                                if (dw != -1 && dws[dw]) { //有这个日期，且要对应的星期日期要进行配置
                                    pq.add(new CronCmd(i, (k * mul + b), crons[i][5]));
                                }
                            }
                        }
                        continue;
                    default:
                        mul *= 100L;
                        Set<Long> tmpList = new HashSet<>();
                        for (Long a : valid) {
                            for (Long b : preList) {
                                tmpList.add(a * mul + b);
                            }
                        }
                        preList = tmpList;
                        break;

                }
            }
        }

    //输出
        while (!pq.isEmpty()){
            CronCmd cronCmd = pq.poll();
            if (cronCmd.dateL >= Long.parseLong(s)  && cronCmd.dateL< Long.parseLong(t) ) //包含start 不包含end
                System.out.println(cronCmd.toString());
        }

    }

    /**
     * 获取年月日对应的星期
     */
    private static int getDayofWeek(int year, int month, int day) {
        DAYSOFMONTHS[2] = 28;
        if (isLeap(year))
            DAYSOFMONTHS[2] = 29;
        if (day > DAYSOFMONTHS[month])   // 该日期不存在
            return -1;

        int sum = 0;
        for (int i = 1971; i <= year; i++) {
            if (isLeap(i-1)){
                sum+=366;
            }else sum+=365;
        }
        for (int i = 1; i < month; i++)
            sum += DAYSOFMONTHS[i];
        sum += day - 1;
        sum += 4; //1970年1月1日是星期四
        return sum % 7;
    }

    //是否是闰年
    static boolean isLeap(int year){
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }

    /**
     * 解析配置信息的年月日星期字符串，解析成数字
     * @param set 解析后结果存储在set里
     * @param str 要解析的字符串
     * @param index 解析的字符串是年/月/日/星期的哪个
     */
    static void setValue(Set<Long> set, String str, int index) {
        if (str.equals("*")) {
            for (int i = 0; i != RANGE[index]; i++)
                if (index == 2 || index == 3)   // 日和月份
                    set.add((long) (i + 1));
                else
                    set.add((long) i);
            return;
        }
        String[] tmp = str.split(",");
        for (String s : tmp) {
            if (s.contains("-")) {
                String[] tmp2 = s.split("-");
                long tmpS = str2Num(tmp2[0]);
                long tmpE = str2Num(tmp2[1]);
                for (long i = tmpS; i <= tmpE; i++) {
                    set.add(i);
                }
            }else {
                set.add(str2Num(s));
            }
        }
    }

    static long str2Num(String str){
        if (str.matches("^[a-zA-Z].*")){ //以字母开头
            return dateMap.get(str.toLowerCase());
        }
        return Long.parseLong(str);
    }

    static int RANGE[] = {60,24,31,12,7};
    static int DAYSOFMONTHS[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    static String WEEKS[] = { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };
    static String MONTHS[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
    static Map<String,Integer> dateMap = new HashMap<>(WEEKS.length+MONTHS.length);

   static class CronCmd implements Comparable<CronCmd>{
       int order; //输入的顺序
        long dateL; //日期
        String cmd; //命令

       public CronCmd(int order,long dateL, String cmd) {
           this.order = order;
           this.dateL = dateL;
           this.cmd = cmd;
       }

       @Override
        public int compareTo(CronCmd o) {
           //题目要求按照时间先后顺序（dateL）输出，如果同一时刻有多条满足条件，则按输入的顺序（order）输出
           if (this.dateL > o.dateL) return 1;
           if (this.dateL < o.dateL) return -1;
           else return Integer.compare(this.order,o.order);


        }

        @Override
        public String toString() {
            return dateL+" "+cmd;
        }
    }
}


